//
//  YAYIListView.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/13.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIListView.h"

@implementation YAYIListView
- (instancetype)initWithView:(UIView *)view
{
    self = [super initWithFrame:view.frame];
    if (self) {
        self.dataAry = [NSMutableArray array];
        _rect = view.frame;
        [self createControl];
        [self createMsgView];
        [self refundsreason];
       
    }
    return self;
}
-(void)createMsgView{
    if (self.backgroundView == nil) {
        self.backgroundView = [UIView new];
        self.backgroundView.userInteractionEnabled = YES;
        self.backgroundView.backgroundColor = [UIColor  whiteColor];
        self.backgroundView.clipsToBounds = YES;
        self.backgroundView.layer.shadowOpacity = 1;
        self.backgroundView.layer.shadowOffset = CGSizeMake(1, 1);
        self.backgroundView.layer.cornerRadius = 3;
        [self addSubview:self.backgroundView];
    }
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(320, 200));
    }];
    
}

-(void)createViews
{
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.tag = 0;
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview:self.cancelBtn];
    [self.cancelBtn  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backgroundView.mas_left).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(40, 20));
        make.top.equalTo(@(10));
    }];
    
    self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.tag = 1;
    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.sureBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview:self.sureBtn];
    [self.sureBtn  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backgroundView.mas_right).with.offset(-10);
        make.size.mas_equalTo(CGSizeMake(40, 20));
        make.top.equalTo(@(10));
    }];

    for (int i =0 ; i < self.dataAry.count; i ++) {
        
        UILabel *label = [UILabel new];
        label.userInteractionEnabled = YES;
        label.text = self.dataAry[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor lightGrayColor];
        [self.backgroundView addSubview:label];
        label.tag = i+1;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [label addGestureRecognizer:tap];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@(40 * i + 40));
            make.size.mas_equalTo(CGSizeMake(320, 40));
            make.left.equalTo(@(0));
        }];
        UILabel *line = [UILabel new];
        line.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
        [self.backgroundView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@(40 * i + 40));
            make.size.mas_equalTo(CGSizeMake(320, 0.5));
            make.left.equalTo(@(0));
        }];
    }
}
-(void)createControl{
    self.control = [[UIControl  alloc] initWithFrame:_rect];
    self.control.backgroundColor = [UIColor lightGrayColor];
    self.control.alpha = 0.2;
    [self addSubview:self.control];
}
-(void)refundsreason
{
    [YAYINetWorking getWithURL:refunds_reason param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if ([dict[@"status"] isEqualToString:@"success"]) {
            NSDictionary *map = dict[@"map"];
            [self.dataAry addObject:map[@"1"]];
            [self.dataAry addObject:map[@"2"]];
            [self.dataAry addObject:map[@"3"]];
            [self.dataAry addObject:map[@"4"]];
             [self createViews];
        }
    }];
 
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    _lastLabel.backgroundColor = [UIColor whiteColor];
    UILabel *label = (UILabel *)tap.view;
    label.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    if (_delegate && [_delegate respondsToSelector:@selector(didselecedRow:content:)]) {
        [_delegate didselecedRow:tap.view.tag content:self.dataAry[tap.view.tag - 1]];
    }
    _lastLabel = label;
    [self layoutIfNeeded];
}
-(void)buttonAction:(UIButton *)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(remoView)]) {
        [_delegate remoView];
    }
     self.hidden = YES;
    [self.control removeFromSuperview];
    [self.backgroundView removeFromSuperview];
    [self removeFromSuperview];

}
@end
